/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "TestConnection.h"

#include "myassert.h"

using namespace std;
using namespace Chronos;

TestConnection::TestConnection(bool* destroyed_)
    : written("")
    , destroyed(destroyed_)
    , output("NULL")
{}


TestConnection::~TestConnection() {
    *destroyed = true;
}


int TestConnection::read (char* buf, unsigned int) {
    ASSERT_STR_NOT_EQUAL("NULL", output);
    int result = 0;
    if (output == "ERROR") {
        result = Connection::ERROR;
    } else if (output == "") {
        result = Connection::NODATA;
    } else if (output == "ENTER") {
        output = "\r\n";
        strcpy(buf, output.c_str());
        result = output.size();
    } else {
        output += "\r\n";
        strcpy(buf, output.c_str());
        result = output.size();
    }
    output = "NULL";
    return result;
}


void TestConnection::checkWritten(const char* expected, const char* caller, int line) {
    Assert::assert_str_equal(expected, written, caller, line);
    clear();
}

